
clear
clear all
clear mata
clear matrix
set more off
set matsize 11000
set maxvar 30000
cap log off
capture log close
set emptycells drop
pause on


***********************************************
* USER DEFINE FILEPATH FOR REPLICATION FOLDER *
***********************************************

global path "C:\Users\wb520443\Dropbox\Research Projects\Global pollution\2_analysis\Replication"



***********************************************


global opts		a f plain coll(none) nodep nomti c(b(star fmt(%9.3f)) se(abs par fmt(%9.3f))  ) star(* .10 ** .05 *** .01) noobs nocons

global opts_fs		a f plain coll(none) nodep nomti c(b(star fmt(%9.3f)) se(abs par fmt(%9.3f))) star(* .10 ** .05 *** .01) noobs nocons


global input "$path\data"
global figdat "$path\figdat"
global figures "$path\figures"
global tables "$path\tables"



cap mat define meanfigure=J(105,6,.)

		local row=1
		
	use "$input/rwi_pollution_analysis_collapsed.dta", replace
	
	encode country, g(country_code)
	
	replace error=1/error
	g both=population*error
	g both2=country_population*error
	
	
	forvalues i=1(1)103{
		
		cap reghdfe mean_pm25 rwi [pweight=both] if country_code==`i', absorb(i.country_code)
			cap mat def meanfigure[`row',1]=_b[rwi]
			cap mat def meanfigure[`row',2]=_se[rwi]	
		
		
		cap reghdfe median_pm25 rwi [pweight=both] if country_code==`i' , absorb(i.country_code)
			cap mat def meanfigure[`row',3]=_b[rwi]
			cap mat def meanfigure[`row',4]=_se[rwi]	
			
		
		cap reghdfe max_pm25 rwi [pweight=both] if country_code==`i', absorb(i.country_code)
			cap mat def meanfigure[`row',5]=_b[rwi]
			cap mat def meanfigure[`row',6]=_se[rwi]	
		local row=`row'+1	
	}
	
	
	
preserve	
		clear
			
		svmat2 meanfigure
		
		g country_code=_n
			run "$input/labels.do"
		g n=_n	
		label val country_code country_code
			decode country_code, g(country)
			labmask n, values(country)

		drop if meanfigure1==.
		
		
		
		
		rename (meanfigure1 meanfigure3 meanfigure5) (mean_beta median_beta max_beta)
	
			twoway (line mean_beta mean_beta, lc(ebblue) lp(dash)) ///
					(scatter median_beta mean_beta, mc(black) ms(D) ///
					xti("Correlation between wealth" "and average pollution",size(large))  ytitle("Correlation between wealth" "and median pollution",size(large)) legend(off) yline(0) xline(0) ylabel(,nogrid labsize(medlarge)) xlabel(,nogrid labsize(medlarge)))
					
					graph export "$figures/mean_vs_median_collapsed.png", as(png) replace
			
			
			sort max_beta
			
			twoway (line max_beta max_beta if country_code!=44, lc(ebblue) lp(dash)) ///
				(scatter max_beta mean_beta if country_code!=44,  mc(black) ms(D) ///
					xti("Correlation between wealth" "and average pollution",size(large))  ytitle("Correlation between wealth" "and max. monthly pollution",size(large)) legend(off) yline(0) xline(0) ylabel(,nogrid labsize(medlarge)) xlabel(,nogrid labsize(medlarge)))
					
					graph export "$figures/mean_vs_max_collapsed.png", as(png) replace
			
restore	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	